package com.zangchat.service.impl;

import com.common.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zangchat.service.IPageInfoService;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageInfoServiceImpl implements IPageInfoService {
    @Override
    public Map<String, Object> initPageInfo(List list){
        Map<String,Object> resultMap=new HashMap();
        PageInfo<Map<String,Object>> listPageInfo = new PageInfo<>(list);
        long total = listPageInfo.getTotal(); // 总条数
        int pages = listPageInfo.getPages(); //总页数
        int nextPage = listPageInfo.getNextPage(); //下一页页数
        boolean hasNextPage = listPageInfo.isHasNextPage(); //是否有下一页
        boolean hasPreviousPage = listPageInfo.isHasPreviousPage(); //是否有上一页
        List<Map<String, Object>> dataList = listPageInfo.getList();//获取的集合数据
        resultMap.put("total",total);
        resultMap.put("pages",pages);
        resultMap.put("nextPage",nextPage);
        resultMap.put("hasNextPage",hasNextPage);
        resultMap.put("hasPreviousPage",hasPreviousPage);
        resultMap.put("list",dataList);
        return resultMap;
    }
    @Override
    public void initPagePageHelper(Integer pageNum,Integer pageSize){
        int pageNums=1;
        int pageSizes=10;
        if (StringUtils.isNotNull(pageNum)){
            pageNums=pageNum;
        }
        if (StringUtils.isNotNull(pageSize) ){
            pageSizes=pageSize;
        }
        PageHelper.startPage(pageNums,pageSizes);
    }
}
